package com.k1ccc0.alumniinformationmanagement.controller;


import com.k1ccc0.alumniinformationmanagement.pojo.Alumni;
import com.k1ccc0.alumniinformationmanagement.pojo.PageBean;
import com.k1ccc0.alumniinformationmanagement.pojo.Result;
import com.k1ccc0.alumniinformationmanagement.service.AlumniService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
public class AlumniController {

    @Autowired
    private AlumniService alumniService;

    //分页查询
    @GetMapping("/Admin/Alumnus")
    public Result selectByPage(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String name, String username, Short gender,
                               String major, String department, String nativePlace, String phoneNumber,
                               String email){
        log.info("分页查询，参数:{},{},{},{},{},{},{},{},{},{}",page,pageSize,name,username,
                gender, major,department,nativePlace,phoneNumber, email);


        PageBean pageBean = alumniService.selectByPage(page, pageSize, name, username, gender, major,
                department, nativePlace, phoneNumber, email);


        return Result.success(pageBean);
    }
    //分页查询
    @GetMapping("/Alumnus")
    public Result selectAllPage(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String name, String username, Short gender,
                               String major, String department, String nativePlace, String phoneNumber,
                               String email){
        log.info("分页查询，参数:{},{},{},{},{},{},{},{},{},{}",page,pageSize,name,username,
                gender, major,department,nativePlace,phoneNumber, email);


        PageBean pageBean = alumniService.selectByPage(page, pageSize, name, username, gender, major,
                department, nativePlace, phoneNumber, email);


        return Result.success(pageBean);
    }

    @PutMapping("/Admin/Alumnus/Update")
    public Result update(@RequestBody Alumni alumni){
        log.info("更新校友信息:{}",alumni);
        alumniService.update(alumni);
        return Result.success();
    }

    @PostMapping("/Admin/Alumnus/Add")
    public Result add(@RequestBody Alumni alumni){
        log.info("添加校友:{}",alumni);
        alumniService.add(alumni);
        return Result.success();
    }

    @GetMapping("/AlumniInfo/{username}")
    public Result selectUserInfo(@PathVariable String username){
        log.info("查询个人信息:{}",username);
        Alumni a = alumniService.selectUserInfo(username);
        return Result.success(a);
    }

    @GetMapping("/GetUserInfo")
    public Result getUserInfo(String username){
        log.info("获取用户名和id:{}",username);
        Alumni alumni = alumniService.selectUserInfoByUAndP(username);
        return Result.success(alumni);
    }

    @PostMapping("/UpdateUsername")
    public Result updateUsernameById(@RequestBody Alumni alumni){
        log.info("用户根据id:{}修改用户名:{}",alumni.getId(),alumni.getUsername());
        alumniService.updateUsernameById(alumni);
        return Result.success();
    }

    @PostMapping("/UpdatePassword")
    public Result updatePasswordById(@RequestBody Alumni alumni){
        log.info("用户根据id:{}修改密码为:{}",alumni.getId(),alumni.getPassword());
        alumniService.updatePasswordById(alumni);
        return Result.success();
    }

    @PostMapping("/Admin/Alumnus/Delete")
    public Result delete(@RequestBody List<Integer> ids){
        log.info("批量删除操作，ids:{}",ids);
        alumniService.delete(ids);
        return Result.success();
    }
    
    @GetMapping("/api/alumni/locations")
    public Result getAlumniLocations() {
        log.info("获取校友地理位置数据用于地图显示");
        List<Alumni> locations = alumniService.getAllAlumniLocations();
        return Result.success(locations);
    }
}
